/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.perutaxi.datos;

import com.perutaxi.entidades.eCliente;
import com.perutaxi.entidades.eEmpresaTaxista;
import com.perutaxi.entidades.eTaxistaReclamo;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author PeruTaxi
 */
public class dTaxistaReclamo implements Serializable {

    public static eTaxistaReclamo inicializar() {
        eTaxistaReclamo taxReclamo = new eTaxistaReclamo();
        taxReclamo.setTaxista(new eEmpresaTaxista());
        taxReclamo.setCliente(new eCliente());
        taxReclamo.setMotivo("");
        taxReclamo.setComentario("");
        taxReclamo.setEstado("");

        return taxReclamo;
    }

    public static eTaxistaReclamo obtenerPorId(Long id) throws Exception {
        eTaxistaReclamo taxReclamo = new eTaxistaReclamo();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_RECLAMO WHERE ID = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                taxReclamo.setId(rs.getLong(1));
                taxReclamo.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxReclamo.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxReclamo.setFechaReclamo(rs.getTimestamp(4));
                taxReclamo.setMotivo(rs.getString(5));
                taxReclamo.setComentario(rs.getString(6));
                taxReclamo.setEstado(rs.getString(7));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return taxReclamo;
    }

    public static List<eTaxistaReclamo> obtenerTodos() throws Exception {
        List<eTaxistaReclamo> lista = new ArrayList<eTaxistaReclamo>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_RECLAMO";
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaReclamo taxReclamo = new eTaxistaReclamo();
                taxReclamo.setId(rs.getLong(1));
                taxReclamo.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxReclamo.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxReclamo.setFechaReclamo(rs.getTimestamp(4));
                taxReclamo.setMotivo(rs.getString(5));
                taxReclamo.setComentario(rs.getString(6));
                taxReclamo.setEstado(rs.getString(7));
                lista.add(taxReclamo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static List<eTaxistaReclamo> obtenerPorIdTaxista(Long id) throws Exception {
        List<eTaxistaReclamo> lista = new ArrayList<eTaxistaReclamo>();
        Connection cnx = Conexion.getConexion();
        String query = "SELECT * FROM TAXISTA_RECLAMO WHERE TAXISTA = " + id;
        try {
            PreparedStatement ps = cnx.prepareCall(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                eTaxistaReclamo taxReclamo = new eTaxistaReclamo();
                taxReclamo.setId(rs.getLong(1));
                taxReclamo.setTaxista(dEmpresaTaxista.obtenerPorId(rs.getLong(2)));
                taxReclamo.setCliente(dCliente.obtenerPorId(rs.getLong(3)));
                taxReclamo.setFechaReclamo(rs.getTimestamp(4));
                taxReclamo.setMotivo(rs.getString(5));
                taxReclamo.setComentario(rs.getString(6));
                taxReclamo.setEstado(rs.getString(7));
                lista.add(taxReclamo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cnx.close();
        }

        return lista;
    }

    public static boolean guardar(eTaxistaReclamo taxReclamo) throws Exception {
        boolean retorno = true;
        Connection cnx = Conexion.getConexion();
        String query = "INSERT INTO TAXISTA_RECLAMO VALUES (?,?,?,?,?,?,?)";
        try {
            PreparedStatement ps = cnx.prepareStatement(query);
            ps.setLong(1, taxReclamo.getId());
            ps.setLong(2, taxReclamo.getTaxista().getId());
            ps.setLong(3, taxReclamo.getCliente().getId());
            ps.setTimestamp(4, taxReclamo.getFechaReclamo());
            ps.setString(5, taxReclamo.getMotivo().toUpperCase());
            ps.setString(6, taxReclamo.getComentario().toUpperCase());
            ps.setString(7, taxReclamo.getEstado().toUpperCase());
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
            retorno = false;
        } finally {
            cnx.close();
        }

        return retorno;
    }
}
